package com.zheng.hospital.api;

import com.zheng.hospital.common.result.CommonResult;
import com.zheng.hospital.common.util.AuthContextHolder;
import com.zheng.hospital.model.user.UserInfo;
import com.zheng.hospital.service.UserService;
import com.zheng.hospital.vo.user.LoginVo;
import com.zheng.hospital.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserService userService;

    /**
     * 用户手机号登录接口
     * @param loginVo
     * @return
     */
    @PostMapping("login")
    public CommonResult login(@RequestBody LoginVo loginVo) {
        Map<String,Object> info = userService.login(loginVo);
        return CommonResult.ok(info);
    }

    /**
     * 用户认证接口
     * @param userAuthVo
     * @param request
     * @return
     */
    @PostMapping("auth/userAuth")
    public CommonResult userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return CommonResult.ok();
    }

    /**
     * 获取用户id信息接口
     * @param request
     * @return
     */
    @GetMapping("auth/getUserInfo")
    public CommonResult getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userService.getById(userId);
        return CommonResult.ok(userInfo);
    }
}
